/*
 * Copyright 2007-2008, www.jsemantic.org, www.adolfoestevez.com,
 * http://semanticj2ee.blogspot.com/ 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jsemantic.core.session;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.jsemantic.core.context.SemanticContext;
import org.jsemantic.core.knowledgedb.KnowledgeDB;
import org.jsemantic.core.mock.MockRuleSession;

/**
 * The Class StatelessMockSemanticSession.
 */
public class StatelessMockSemanticSession implements SemanticSession {

	/** The ctx. */
	private SemanticContext ctx = null;

	/** The db. */
	private KnowledgeDB db = null;

	/**
	 * Instantiates a new stateless mock semantic session.
	 * 
	 * @param ctx the ctx
	 * @param db the db
	 */
	public StatelessMockSemanticSession(SemanticContext ctx, KnowledgeDB db) {
		this.ctx = ctx;
		this.db = db;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jsemantic.core.session.SemanticSession#dispose()
	 */
	public void dispose() {
		ctx.dispose();
		db.dispose();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jsemantic.core.session.SemanticSession#execute(java.lang.Object)
	 */
	public Collection execute(Object o) throws SemanticException {

		Collection facts = new ArrayList();

		if (db != null) {
			Iterator i = db.getKnowledge();
			while (i.hasNext()) {
				facts.add(i.next());
			}
		}

		facts.add(o);

		Collection results = ((MockRuleSession) ctx.getRuleSession())
				.execute(facts);

		facts.clear();
		facts = null;
		return results;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jsemantic.core.session.SemanticSession#execute(java.util.Collection)
	 */
	public Collection execute(Collection o) throws SemanticException {

		Collection facts = new ArrayList();

		if (db != null) {
			Iterator i = db.getKnowledge();
			while (i.hasNext()) {
				facts.add(i.next());
			}
		}

		facts.addAll(o);

		Collection results = ((MockRuleSession) ctx.getRuleSession())
				.execute(facts);

		facts.clear();
		facts = null;
		return results;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jsemantic.core.session.SemanticSession#fireAllRules()
	 */
	public void fireAllRules() throws SemanticException {
		((MockRuleSession) ctx.getRuleSession()).fireAllRules();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jsemantic.core.session.SemanticSession#getContext()
	 */
	public SemanticContext getContext() {
		return this.ctx;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jsemantic.core.session.SemanticSession#insert(java.lang.Object)
	 */
	public Object insert(Object fact) throws SemanticException {
		return this.execute(fact);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jsemantic.core.session.SemanticSession#insert(java.util.Collection)
	 */
	public Collection insert(Collection facts) throws SemanticException {
		return this.execute(facts);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jsemantic.core.session.SemanticSession#modifyInsert(java.lang.Object,
	 *      java.lang.Object)
	 */
	public void modifyInsert(Object handle, Object arg)
			throws SemanticException {
		throw new OperationNotSupported("Operation Not Supported");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jsemantic.core.session.SemanticSession#modifyRetract(java.lang.Object)
	 */
	public void modifyRetract(Object handle) throws SemanticException {
		throw new OperationNotSupported("Operation Not Supported");
	}

	/* (non-Javadoc)
	 * @see org.jsemantic.core.session.SemanticSession#getKnowledgeDB()
	 */
	public KnowledgeDB getKnowledgeDB() {
		return db;
	}

	/* (non-Javadoc)
	 * @see org.jsemantic.core.session.SemanticSession#isStateful()
	 */
	public boolean isStateful() {
		return false;
	}
}
